//
//  CompleteOrderListProxy.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "CompleteOrderListProxy.h"
#import "JFBCompleteOrderDS.h"
#import "JFBDailyDS.h"
#import "CompleteOrderCell.h"
#import "CompleteDailyCell.h"
#import "JFBLoadingAnimation.h"

@implementation CompleteOrderListProxy

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame orderType:(NSString * )orderType {
    self = [super initWithFrame:frame orderType:orderType];
    if (self) {
//        self.tableView.tableHeaderView = nil;
        return self;
    }
    return self;
}

-(void)loadNewData {
    if (self.dateInfo == nil) {
        [JFBCompleteOrderDS loadOrderList:^(NSArray *orders, NSError *error) {
            [self.tableView.header endRefreshing];
              self.items = [@[] mutableCopy];
            if (error || orders.count == 0) {
                [ComponentsUtils emptyStringTips:self.tableView tips:@"当前没有订单，继续努力哦～"];
            } else {
                if( self.items.count == 0 ) {
                    [ComponentsUtils cleanEmptyTips:self.tableView];
                }
                _cellFrame = [[NSMutableDictionary alloc] init];
              
                [self.items addObjectsFromArray:orders];
            }
            [self.tableView reloadData];
        } orderType:kOrderStatusComplete pager:self.pager];
    }
    else
    {
        [JFBCompleteOrderDS loadDayDetailList:^(NSArray *orders, NSError *error) {
            [self.tableView.header endRefreshing];
             self.items = [@[] mutableCopy];
            if (error || orders.count == 0) {
                [ComponentsUtils emptyStringTips:self.tableView tips:@"当前没有订单，继续努力哦～"];
            } else {
                if( self.items.count == 0 ) {
                    [ComponentsUtils cleanEmptyTips:self.tableView];
                }
                _cellFrame = [[NSMutableDictionary alloc] init];
               
                [self.items addObjectsFromArray:orders];
            }
            [self.tableView reloadData];
        } so_status:kOrderStatusComplete day: self.dateInfo];
    }
}



- (void)reload:(__unused id)sender {
    JFBLoadingAnimation * animation = [[JFBLoadingAnimation alloc] init:self.tableView];
    if (self.dateInfo == nil)
    {
        [JFBCompleteOrderDS loadOrderList:^(NSArray *orders, NSError *error) {
            [animation stop];
            self.items = [@[] mutableCopy];
            if (error || orders.count == 0) {
                [ComponentsUtils emptyStringTips:self.tableView tips:@"当前没有订单，继续努力哦～"];
            } else {
                if( self.items.count == 0 ) {
                    [ComponentsUtils cleanEmptyTips:self.tableView];
                }
                _cellFrame = [[NSMutableDictionary alloc] init];
                
    //            [ComponentsUtils cleanEmptyTips:self.tableView];
                [self.items addObjectsFromArray:orders];
                [self.tableView reloadData];
            }
        } orderType:kOrderStatusComplete pager:self.pager];
    }
    else
    {
        [JFBCompleteOrderDS loadDayDetailList:^(NSArray *orders, NSError *error) {
            [animation stop];
            self.items = [@[] mutableCopy];
            if (error || orders.count == 0) {
                [ComponentsUtils emptyStringTips:self.tableView tips:@"当前没有订单，继续努力哦～"];
            } else {
                if( self.items.count == 0 ) {
                    [ComponentsUtils cleanEmptyTips:self.tableView];
                }
                _cellFrame = [[NSMutableDictionary alloc] init];
                
                [self.items addObjectsFromArray:orders];
                [self.tableView reloadData];
            }
        } so_status:kOrderStatusComplete day: self.dateInfo];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSObject *obj = self.items[indexPath.row];
    if( [obj isKindOfClass:[JFBDailyDS class]] ) {
        return 34;
    } else {
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    NSObject *obj = self.items[indexPath.row];
    if( [obj isKindOfClass:[JFBDailyDS class]] ) {
        static NSString *cellIdentifier = @"OrderDailyCellIdentifier";
        cell= [[CompleteDailyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier frame:self.tableView.bounds];
        ((CompleteDailyCell*)cell).daily = (JFBDailyDS*)obj;
    } else {
        static NSString *cellIdentifier = @"OrderCompleteCellIdentifier";
        cell= [[CompleteOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier frame:self.tableView.bounds];
        ((CompleteOrderCell*)cell).order = (JFBCompleteOrderDS*)obj;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end
